from flask import render_template, redirect, url_for, session, request, g

from models import db
from models.index import User, Product, Category, Review, Cart
from . import index_blu


@index_blu.route("/")
def index():
    products = db.session.query(Product).order_by(-Product.sales).limit(8)
    if not g.user:
        return render_template("index.html", products=products)
    p_in_cart = g.user.my_cart
    count_my_cart = len(g.user.my_cart)
    return render_template("index.html", user=g.user, products=products, p_in_cart=p_in_cart,
                           count_my_cart=count_my_cart)


@index_blu.route("/index.html")
def redirect_to_index():
    return redirect(url_for("index.index"))


@index_blu.route("/product.html")
def product():
    page = request.args.get('page', 1)
    c_id = request.args.get('c_id', "0")
    c_types = db.session.query(Category).all()
    print(page)
    print(c_id)
    if c_id == "0":
        # print("page", page)
        # print('c_id', c_id)
        paginate = db.session.query(Product).paginate(
            page=int(page), per_page=6, error_out=False)
        if not g.user:
            return render_template("product.html", paginate=paginate, c_types=c_types, c_id=c_id)
        count_my_cart = len(g.user.my_cart)
        return render_template("product.html", paginate=paginate, c_types=c_types, user=g.user,
                               p_in_cart=g.user.my_cart, count_my_cart=count_my_cart, c_id=c_id)

    paginate = db.session.query(Product).filter(Product.category_id == c_id).paginate(page=int(page), per_page=6,
                                                                                      error_out=False)
    if not g.user:
        return render_template("product.html", paginate=paginate, c_types=c_types, c_id=c_id)
    count_my_cart = len(g.user.my_cart)
    return render_template("product.html", paginate=paginate, c_types=c_types, user=g.user, p_in_cart=g.user.my_cart,
                           count_my_cart=count_my_cart, c_id=c_id)


@index_blu.route("/product-single.html/<int:p_id>")
def product_single(p_id):
    product = db.session.query(Product).filter(Product.id == p_id).first()
    reviews = db.session.query(Review).filter(Review.product_id == p_id).all()
    # for review in reviews:
    #     print(review.id)
    if not g.user:
        return render_template("product-single.html", product=product, reviews=reviews)
    count_my_cart = len(g.user.my_cart)
    return render_template("product-single.html", product=product, reviews=reviews, user=g.user,
                           count_my_cart=count_my_cart, p_in_cart=g.user.my_cart)


@index_blu.route("/about.html")
def about():
    if not g.user:
        return render_template("about.html")
    p_in_cart = g.user.my_cart
    count_my_cart = len(g.user.my_cart)
    return render_template("about.html", user=g.user, p_in_cart=p_in_cart, count_my_cart=count_my_cart)


@index_blu.route("/contact.html")
def contact():
    if not g.user:
        return render_template("contact.html")
    p_in_cart = g.user.my_cart
    count_my_cart = len(g.user.my_cart)
    return render_template("contact.html", user=g.user, p_in_cart=p_in_cart, count_my_cart=count_my_cart)
